import { _decorator, Component, Node, Label, tween, view, UITransform, Vec3, SpriteAtlas, SpriteFrame, Sprite, UIOpacity, EventTouch, Toggle, Prefab, instantiate } from 'cc';
import { App } from '../../App';
import { EventConst, localKey } from '../../common/Common';
import { LoadMg } from '../../tools/LoadMg';
import { showTip } from '../../tools/PopuUp';
import UtilTool from '../../tools/UtilTool';
const { ccclass, property } = _decorator;

// 幸运球界面
@ccclass('LBBallItemPanel')
export class LBBallItemPanel extends Component {
    @property(Node)
    sel: Node = null;

    @property(Node)
    sel_open: Node = null;

    @property(Node)
    ball: Node = null;

    private parentObj = null
    private index = null

    // 状态
    private status = 0 // [0]可下注状态 [1]已下注状态 [2]已下注且中奖状态

    onLoad() {
        App.Event.on(EventConst.UPDATE_MODE1_BALL,this.updata, this )

        App.Event.on(EventConst.UPDATE_MODE1_BET, this.updataOne,this)
    }

    start() {
    }

    /** 
     * 创建球体
      */
    setData(data,obj) {
        this.parentObj = obj
        this.index = data.index
        this.status = 0

        this.sel.active = data.isBet
        this.sel_open.active = false
        this.ball.getComponent(Sprite).spriteFrame = this.parentObj.ballArr[this.index]
    }

    /** 
     * 更新信息
     * @param status 当前游戏状态 0下单中 1倒计时 2击球中
     * @param nums 本期中奖数字（只有在击球阶段才会有）
      */
    updata(list, status,nums) {
        this.sel.active = false
        this.sel_open.active = false
        this.status = 0

        for (let i = 0; i < list.length; i++) {
            let data = list[i]
            if (parseInt(data.nums) == this.index) {
                if(status==2 && this.index==nums) { // 击球中状态而且是中奖的那个球的处理流程
                    this.status = 2
                    this.sel_open.active = true
                }
                else{
                    this.sel.active = true
                    this.status = 1
                }

                break
            }
        }
    }

    /** 
     * 下注后更新信息
      */
    updataOne(data) {
        if (parseInt(data.nums) != this.index) return
        this.sel.active = true
        this.status = 1

        this.parentObj.gameData.list.push({
            id: data.id,
            nums: data.nums,
            value: data.value,
            hitStatus:0
        })
    }

    /** 
     * 点击去下注
      */
    clickGotoBet() {
        if (this.parentObj.gameData.status != 0) {  // 非下注阶段
            showTip(App.language.getLanguageLab(46))
            return
        }

        if (this.status == 0) { // 未下注
            this.parentObj.showBetPanel(this.index)
        }
        else {  // 已下注
            showTip(App.language.getLanguageLab(40))
        }
    }

    onDestroy() {
        App.Event.off(EventConst.UPDATE_MODE1_BALL,this.updata, this)
        App.Event.off(EventConst.UPDATE_MODE1_BET,this.updataOne, this)
    }

}

